<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/11/19
 * Time: 下午1:48
 */

////--基础维护
////发动机机油 221 Engine oil
//jd_car_product_item_engine_oil_221 -- ok
////机油滤清器 211 Oil filter
//jd_car_product_item_oil_filter_211 -- ok
////空气滤清器 209 Air cleaner
//jd_car_product_item_air_cleaner_209 -- ok
////空调滤清器 213 Air conditioning filter
//jd_car_product_item_air_conditioning_filter_213 -- ok
////燃油滤清器 212 Fuel filter
//jd_car_product_item_fuel_filter_212 -- ok
////火花塞 214 Spark plug
//jd_car_product_item_spark_plug_214 -- ok
////--轮胎专区
////前轮胎 218 Front tire
//jd_car_product_item_front_tyre_218
////后轮胎 219 Rear tyre
//jd_car_product_item_rear_tyre_219
////--蓄电池
////蓄电池 233 Battery
//jd_car_product_item_battery_233
////--制动系统
////前刹车片 235 Front brake pad
//jd_car_product_item_front_brake_pad_235
////后刹车片 236 Rear brake pad
//jd_car_product_item_rear_brake_pad_236
////刹车油 249 Brake oil
//jd_car_product_item_brake_oil_249
////前刹车盘 251 Front brake disc
//jd_car_product_item_front_brake_disc_251
////后刹车盘 252 Rear brake disc
//jd_car_product_item_rear_brake_disc_252
////--检修养护件
////主雨刷 197 The main wiper
//jd_car_product_item_main_wiper_197
////副雨刷 222 Deputy wiper
//jd_car_product_item_deputy_wiper_222
////汽油添加剂 220 Gasoline additive
//jd_car_product_item_gasoline additive_220
////防冻液 248 Antifreeze
//jd_car_product_item_antifreeze_248
////贴膜 250 Film
//jd_car_product_item_film_250
////--照明系统
////近光灯 245 dipped lamp
//jd_car_product_item_dipped_lamp_245
////远光灯 246 High lamp
//jd_car_product_item_high_lamp_246
////雾灯 247 Fog lamp
//jd_car_product_item_fog_lamp_247

$api_service_path = __DIR__ . "/../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();

$cars = DB::table('jd_car_data')
    ->where('id','>=','17890')
    ->select('id','car_model_id')
    ->get();
foreach($cars as $_car){
    $pageIndex = 1;
    $itemNum = 1;
    $total = 1;
    while(1){
        $product = queryProduct('218',$_car->car_model_id,$pageIndex);
        if($product->code == 'success'){
            $data = $product->data;
            $total = $product->total;
            if(count($data) == 0){
                echo $_car->id . ' ' . $product->code . "\n";
                $jd_car_product_item = array(
                    'jd_car_id' => $_car->id,
                    'car_model_id' => $_car->car_model_id,
                    'product_id' => 0,
                    'product_name' => '无匹配信息',
                    'product_picUrl' => ''
                );
                try{
                    DB::table('jd_car_product_item_front_tyre_218')->insert($jd_car_product_item);
                    break;
//                    var_dump($jd_car_product_item);
                }catch (Exception $e) {
                    echo "insert into jd_car_product_item_front_tyre_218 error\n";
                    var_dump($e);
                }
            }else{
                foreach($data as $_data){
                    $jd_car_product_item = array(
                        'jd_car_id' => $_car->id,
                        'car_model_id' => $_car->car_model_id,
                        'product_id' => $_data->id,
                        'product_name' => $_data->name,
                        'product_picUrl' => $_data->picUrl
                    );
                    try{
                        DB::table('jd_car_product_item_front_tyre_218')->insert($jd_car_product_item);
//                        echo 'total = ' . $total . ' pageIndex = ' . $pageIndex . ' itemNum = ' . $itemNum . "\n";
                        $itemNum = $itemNum + 1;
//                        var_dump($jd_car_product_item);
                    }catch (Exception $e) {
                        echo "insert into jd_car_product_item_front_tyre_218 error\n";
                        var_dump($e);
                    }
                }
                if($total > $itemNum){
                    $pageIndex = $pageIndex + 1;
                }else{
                    break;
                }
            }
        }else{
            echo $_car->id . ' ' . $product->code . "\n";
            $jd_car_product_item = array(
                'jd_car_id' => $_car->id,
                'car_model_id' => $_car->car_model_id,
                'product_id' => 0,
                'product_name' => '无匹配信息',
                'product_picUrl' => ''
            );
            try{
                DB::table('jd_car_product_item_front_tyre_218')->insert($jd_car_product);
                break;
//                var_dump($jd_car_product_item);
            }catch (Exception $e) {
                echo "insert into jd_car_product_item_front_tyre_218 error\n";
                var_dump($e);
            }
        }
        usleep(500);
    }
//    exit;
}

function queryProduct($accyCatId,$car_model_id,$pageIndex){
    $url = 'http://auto.jd.com/queryProduct?accyCatId='.$accyCatId.'&modelId='.$car_model_id.'&pageIndex='.$pageIndex;
    while(1){
        try{
            $response = Httpful::get($url)
                ->timeoutIn(5)
                ->sendsForm()->send();
            break;
        }catch (Exception $e) {
            var_dump($e);
        }
    }
    return $response->body;
}